<?php
namespace app\admin\service;

use app\admin\model\NavGroup;
use app\admin\model\NavItem;
use app\admin\service\interface\NavService;
class NavServiceImpl implements NavService{
    public function navAdd(){

    }
    public function list($withCrumbs=false){
        $res=[];
        $groups=NavGroup::order('display_order desc')->select();
        foreach($groups as $k=>$v){
            $group = $v->toArray();
            $group['items']=$v->items()->order('display_order desc')->column('*','id');
            if($withCrumbs){
                foreach ($group['items'] as $key => &$item) {
                    $this->buildListItems($item);
                }
            }
            $res[]=$group;
        }
        return $res;
    }
    private function buildListItems(&$item)
    {
        $item['items']=NavItem::where('pre_id',$item['id'])->order('display_order desc')->column('*','id');
        foreach ($item['items'] as $key => &$value) {
            # code...
            $list = NavItem::where('pre_id',$value['id'])->order('display_order desc')->select();
            if (!$list->isEmpty()) {
                $this->buildListItems($value);
            }
        }
    }
    public function navRemove(){

    }
    public function navUpdate(){

    }
    public function groupAdd(){

    }
    public function groupRemove(){

    }
    public function groupUpdate(){

    }
    public function moveNav(int $nav_id,int $group_id){

    }
    public function buildCacheData():array{
        $res=[];
        $res['navs_by_path']=NavItem::order('display_order desc')->column('*','path');
        $res['navs_by_id']=NavItem::order('display_order desc')->column('*','id');
        $res['groups']=[];
        $groups=NavGroup::order('display_order desc')->select();
        foreach($groups as $k=>$v){
            $res["groups"][$v->id]=$v->toArray();
            $res["groups"][$v->id]['items']=$v->items()->order('display_order desc')->column('*','tag');
        }
        return $res;
    }
}